package com.songshuai.webapi.crm.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.dto.feign.response.AccountInfoListResDto;
import com.songshuai.webapi.baseapi.utils.OssUploadUtil;
import com.songshuai.webapi.baseapi.utils.RequestParamValidUtil;
import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import com.songshuai.webapi.common.upload.BaseUploadFile;
import com.songshuai.webapi.crm.dto.request.customer.AddCustomerInfoReqDto;
import com.songshuai.webapi.crm.dto.request.customer.AddPageListColumnsConfigReqDto;
import com.songshuai.webapi.crm.dto.request.customer.CustomerListReqDto;
import com.songshuai.webapi.crm.dto.response.customer.CustomerExportDto;
import com.songshuai.webapi.crm.dto.response.customer.CustomerListResDto;
import com.songshuai.webapi.crm.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @Author:wbl
 * @Date: 2018/12/28
 */
@Slf4j
@Api("学员信息")
@RestController
@RequestMapping("/api/customer/")
public class CustomerController extends BaseController{

    @Autowired
    private CustomerService customerService;




    @ApiOperation("新增学员")
    @RequestMapping(value = "addCustomer", method = RequestMethod.POST)
    public QueryResponse addCustomer(@Valid @RequestBody AddCustomerInfoReqDto parmDto, BindingResult result) {
        if(result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        boolean isSucc = customerService.addCustomer(parmDto);;
        if(isSucc) {
            return new QueryResponse();
        }
        return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }

    @ApiOperation("查询学员")
    @RequestMapping(value = "queryCustomerInfoListByParam", method = RequestMethod.POST)
    public QueryResponse<List<CustomerListResDto>> queryCustomerInfoListByParam(@RequestBody BaseParmDto<CustomerListReqDto> parmDto) {
        String orderBy = parmDto.getSortBy() + " " + parmDto.getSort();
        Page<Object> page = PageHelper.startPage(parmDto.getPageIndex(), parmDto.getPageSize(),orderBy);
        List<CustomerListResDto> customerList = customerService.queryCustomerInfoListByParam(parmDto.getParam());
        return QueryPageRes.setQueryResponseData(customerList, page.getTotal());
    }

    @ApiOperation("今日待跟进总计")
    @RequestMapping(value = "queryFollowCustomerCount", method = RequestMethod.GET)
    public QueryResponse<Integer> queryFollowCustomerCount(){
        Integer count = customerService.queryFollowCustomerCount();
        return QueryResponse.setResData(count);
    }

    @ApiOperation("查询今日待跟进")
    @RequestMapping(value = "queryFollowCustomerList", method = RequestMethod.POST)
    public QueryResponse<List<CustomerListResDto>> queryFollowCustomerList(@RequestBody BaseParmDto parmDto) {
        String orderBy = "";
        if(StringUtils.isBlank(parmDto.getSortBy())){
            orderBy = orderBy + "create_time" + " ";
        } else {
            orderBy = parmDto.getSortBy() + " ";
        }
        if(StringUtils.isBlank(parmDto.getSort())){
            orderBy = orderBy + "desc";
        } else {
            orderBy = parmDto.getSort();
        }
        Page<Object> page = PageHelper.startPage(parmDto.getPageIndex(), parmDto.getPageSize(),orderBy);
        List<CustomerListResDto> customerList = customerService.queryFollowCustomerList();
        return QueryPageRes.setQueryResponseData(customerList, page.getTotal());
    }

    @ApiOperation("查询配置")
    @RequestMapping(value = "queryPageListColumnsConfig",method = RequestMethod.GET)
    public QueryResponse<String> queryPageListColumnsConfig(@RequestParam("configType") Integer configType){
        String config = customerService.queryPageListColumnsConfig(configType);
        return QueryResponse.setResData(config);
    }

    @ApiOperation("新增/修改配置")
    @RequestMapping(value = "addOrUpdatePageListColumnsConfig", method = RequestMethod.POST)
    public QueryResponse addOrUpdatePageListColumnsConfig(@Valid @RequestBody AddPageListColumnsConfigReqDto parmDto, BindingResult result) {
        if(result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        boolean isSucc = customerService.addOrUpdatePageListColumnsConfig(parmDto);;
        if(isSucc) {
            return new QueryResponse();
        }
        return new QueryResponse(ResultStatus.ERROR_UPDATE_FAILED);
    }

    @ApiOperation("根据校区查询用户信息")
    @RequestMapping(value = "queryUserInfoListByCampusId", method = RequestMethod.GET)
    public QueryResponse<List<AccountInfoListResDto>> queryUserInfoListByCampusId() {
        return QueryResponse.setResData(customerService.queryUserInfoListByCampusId());
    }

    @ApiOperation("根据真实姓名模糊查询用户信息")
    @RequestMapping(value = "queryUserInfoListByTrueName", method = RequestMethod.GET)
    public QueryResponse<List<AccountInfoListResDto>> queryUserInfoListByTrueName(@RequestParam("trueName")String trueName) {
        return QueryResponse.setResData(customerService.queryUserInfoListByTrueName(trueName));
    }

    @ApiOperation("导出学员")
    @RequestMapping(value = "exportCustomerInfoByParam", method = RequestMethod.POST)
    public QueryResponse<CustomerExportDto> exportCustomerInfoByParam(@RequestBody BaseParmDto<CustomerListReqDto> parmDto, HttpServletResponse response){
        try {
            return customerService.exportExcel(parmDto.getParam());
        }catch (Exception e){
            log.error("api/customer/exportCustomerInfoByParam", e);
            return new QueryResponse(ResultStatus.ERROR_FILE_DOWNLOAD);
        }
    }

}
